import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 默认请求的基础地址
const request = axios.create({
  timeout: 5000
})

request.interceptors.request.use(function (config) {
  NProgress.start();

  // 判断是否需携带token
  const token = sessionStorage.getItem('token_key')
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, function (error) {
  return Promise.reject(error);
})


// 响应拦截器
request.interceptors.response.use(function (response) {
  NProgress.done();
  return response.data;
}, function (error) {
  NProgress.done();
  console.dir(error)
  return new Promise(() => { })
})

export default request